
package ${packageName};

<#list imports as import>
import ${import};
</#list>
import ${basePackage}.vo.${underlineToCamel(sourceTable?lower_case,true?string)}Vo;
import ${basePackage}.entity.${underlineToCamel(sourceTable?lower_case,true?string)}Entity;
import ${basePackage}.service.${underlineToCamel(sourceTable?lower_case,true?string)}Service;
import ${basePackage}.param.${underlineToCamel(sourceTable?lower_case,true?string)}Param;
import com.intretech.umsin.common.excel.service.ExcelService;
import org.apache.commons.io.IOUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;

import java.util.UUID;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
* ${sourceTableDesc}:${sourceTable} 对应的API访问类.
* @ClassName: ${underlineToCamel(sourceTable?lower_case,true?string)}Controller
* @Description: ${sourceTableDesc}:${sourceTable} 对应的API访问类.
* @author: ${author}
* @date:   ${.now}
* @version V1.0
*/
@RestController
@RequestMapping("${pathName}")
@Api(tags = "${sourceTableDesc} API")
public class ${underlineToCamel(sourceTable?lower_case,true?string)}Controller {


    @Autowired
    private ${underlineToCamel(sourceTable?lower_case,true?string)}Service ${underlineToCamel(sourceTable?lower_case,false?string)}Service;

    @Autowired
    private ExcelService excelService;

    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询单个${sourceTableDesc}对象", notes = "查询单个${sourceTableDesc}对象")
    public ApiResult<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> query${underlineToCamel(sourceTable?lower_case,true?string)}ById(@PathVariable("id") String id){
        Function<String,${underlineToCamel(sourceTable?lower_case,true?string)}Vo> fun = p -> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.queryById(p);
        return ApiResult.responseFor(fun,id);
    }

    @PostMapping("/listByIds")
    @ApiOperation(value = "根据ID列表查询${sourceTableDesc}集合对象", notes = "根据ID列表查询${sourceTableDesc}集合对象")
    public ApiResult<List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> query${underlineToCamel(sourceTable?lower_case,true?string)}ByIds(@RequestBody List<String> ids){
        Function<List<String>,List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> fun = p -> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.queryByIds(p);
        return ApiResult.responseFor(fun,ids);
    }

    @PostMapping("/page/{pageNo}/{pageSize}")
    @ApiOperation(value = "查询返回${sourceTableDesc}分页对象", notes = "查询返回${sourceTableDesc}分页对象")
    public ApiResult<IPage<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> queryForPage(@RequestBody ${underlineToCamel(sourceTable?lower_case,true?string)}Param params, @PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize){
        BiFunction<IPage,${underlineToCamel(sourceTable?lower_case,true?string)}Param,IPage<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> fun = (p1, p2) -> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.queryForPage(p1, p2);
        return ApiResult.responseFor(fun,new Page(pageNo,pageSize), params);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询${sourceTableDesc}对象列表", notes = "查询返回${sourceTableDesc}对象列表")
    public ApiResult<List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> queryForList(@RequestBody ${underlineToCamel(sourceTable?lower_case,true?string)}Param params){
        Function<${underlineToCamel(sourceTable?lower_case,true?string)}Param,List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> fun = p -> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.queryForList(p);
        return ApiResult.responseFor(fun,params);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增${sourceTableDesc}对象", notes = "新增${sourceTableDesc}对象")
    public ApiResult<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> add(@RequestBody ${underlineToCamel(sourceTable?lower_case,true?string)}Vo vo){
        Function<${underlineToCamel(sourceTable?lower_case,true?string)}Vo,${underlineToCamel(sourceTable?lower_case,true?string)}Vo> fun = p -> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.add(p);
        return ApiResult.responseFor(fun,vo);
    }

    @PostMapping("/adds")
    @ApiOperation(value = "新增${sourceTableDesc}对象列表", notes = "新增${sourceTableDesc}对象列表")
    public ApiResult<List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> adds(@RequestBody List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> voList){
        Function<List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>,List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> fun = p -> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.adds(p);
        return ApiResult.responseFor(fun,voList);
    }

    @PutMapping("/modify")
    @ApiOperation(value = "更新${sourceTableDesc}对象", notes = "更新${sourceTableDesc}对象")
    public ApiResult<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> modify(@RequestBody ${underlineToCamel(sourceTable?lower_case,true?string)}Vo vo){
        Function<${underlineToCamel(sourceTable?lower_case,true?string)}Vo,${underlineToCamel(sourceTable?lower_case,true?string)}Vo> fun = p -> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.modify(p);
        return ApiResult.responseFor(fun,vo);
    }

    @PutMapping("/modifys")
    @ApiOperation(value = "更新${sourceTableDesc}对象", notes = "更新${sourceTableDesc}对象")
    public ApiResult<List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> modifys(@RequestBody List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> voList){
        Function<List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>,List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo>> fun = p-> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.modifys(p);
        return ApiResult.responseFor(fun,voList);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除单个${sourceTableDesc}对象", notes = "删除单个${sourceTableDesc}对象")
    public ApiResult<Void> removeUser(@PathVariable("id") String id){
        Consumer<String> consumer = p -> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.removeById(p);
        return ApiResult.responseFor(consumer,id);
    }

    @DeleteMapping("/removes")
    @ApiOperation(value = "批量删除${sourceTableDesc}对象", notes = "批量删除${sourceTableDesc}对象")
    public ApiResult<Void> removeUsers(@RequestBody List<String> ids){
        Consumer<List<String>> consumer = p -> ${underlineToCamel(sourceTable?lower_case,false?string)}Service.removeBatchByIds(p);
        return ApiResult.responseFor(consumer, ids);
    }

   @PostMapping("/export")
   @ApiOperation(value = "${sourceTableDesc}对象导出Excel", notes = "${sourceTableDesc}对象导出Excel")
   public ApiResult<Void> exportExcel(@RequestBody ${underlineToCamel(sourceTable?lower_case,true?string)}Param params , HttpServletResponse response) throws Exception {
       Consumer<${underlineToCamel(sourceTable?lower_case,true?string)}Param> consumer = p ->{
           try {
               List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> ls = ${underlineToCamel(sourceTable?lower_case,false?string)}Service.queryForList(p);
               if(!CollectionUtils.isEmpty(ls)){
                   byte[] data = excelService.exportExcel(ls, ${underlineToCamel(sourceTable?lower_case,true?string)}Vo.class,"${sourceTableDesc}");
                   response.reset();
                   //设置响应头
                   response.setHeader("Content-Disposition", "attachment; filename=\""+ UUID.randomUUID().toString().replace("-","").toUpperCase() +".xls\"");
                   response.addHeader("Content-Length", "" + data.length);
                   response.setContentType("application/msexcel; charset=UTF-8");
                   IOUtils.write(data, response.getOutputStream());
               }
           }catch (Exception e){
                throw new RuntimeException("export excel failed");
           }
       };
       return ApiResult.responseFor(consumer, params);
   }

}